package org.andengine.shmup;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.pool.GenericPool;


/**
 * Class for holding a pool of animated Sprites
 * 
 * @author Isaiah
 * Last update 7/20/2011
 */
public class AnimatedSpritePool extends GenericPool<AnimatedLaser> {

	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================
	
	private TiledTextureRegion animSprite;
	private long[] durations;
	private int first;
	private int last;
	private boolean loop;

	// ===========================================================
	// Constructors
	// ===========================================================
	
	AnimatedSpritePool(TiledTextureRegion animSprite){
		this.animSprite = animSprite;
		this.batchAllocatePoolItems(40);
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	
	/**
	 * Sets how the sprites that come out will be animated
	 */
	public void setAnimation(long[] durations, int first, int last, boolean loop){
		this.durations = durations;
		this.first = first;
		this.last = last;
		this.loop = loop;
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	//Called When a new sprite is needed 
	@Override
	protected AnimatedLaser onAllocatePoolItem() {
		return new AnimatedLaser(800, 800, animSprite);
	}
	
	//Called when a sprite is sent to the pool
	@Override
	protected void onHandleRecycleItem(final AnimatedLaser animSprite){
		if(this.getUnrecycledCount() < 2) {
            animSprite.setPosition(800, 800);
            animSprite.setMode(0);
            animSprite.setAngle(0);
            animSprite.deactivate();
            animSprite.stopAnimation();
		}
	}
	
	//Called just before it is sent to the caller
	@Override
	protected void onHandleObtainItem(final AnimatedLaser animSprite) {
		// TODO Make a more generic manner for setting how the sprite will come out
		//animSprite.animate(durations, first, last, loop);
		animSprite.stopAnimation(13);
		animSprite.activate();
	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
